/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.scene.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.scene.service.api.ProjectSafetyMeetingService;
import com.koron.scene.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "ProjectSafetyMeeting", description = "项目相关方-相关方安全会议")
@RestController
@RequestMapping("/projectSafetyMeeting")
public class ProjectSafetyMeetingController {
    private final static  Logger logger = LoggerFactory.getLogger(ProjectSafetyMeetingController.class);

    @Autowired
    private ProjectSafetyMeetingService service;

    @ApiOperation("相关方安全会议导入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "相关方id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/importExcel.htm", method = {RequestMethod.POST})
    @Log(title = "相关方安全会议导入", businessManipulation = BusinessManipulation.IMPORT)
    public MessageBean<?> importExcel(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                      @RequestParam(value = "file") MultipartFile file, @ApiIgnore String projectId, @ApiIgnore String enterpriseId) {

        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.importExcel(user, file, projectId, enterpriseId);
    }


    @ApiOperation("导出相关方安全会议")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "meetingName", value = "会议名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "相关方id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/export.htm", method = RequestMethod.GET)
    @Log(title = "导出相关方安全会议", businessManipulation = BusinessManipulation.EXPORT)
    public HttpEntity<?> export(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String meetingName, @ApiIgnore String projectId, @ApiIgnore String enterpriseId) {
        if (null == user) {
            return null;
        }
        List<Map> list = service.exportForList(meetingName, projectId, enterpriseId);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        File file = new File("static/excel/ProjectSafetyMeeting.xlsx");
        try (
                InputStream inputStream = getClass().getClassLoader().getResourceAsStream("static/excel/ProjectSafetyMeeting.xlsx");
        ) {
            FileUtils.copyInputStreamToFile(inputStream, file);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("相关方安全会议", file, "ProjectSafetyMeeting.btl", map);
    }

    @ApiOperation(value = "下载相关方安全会议excel模板")
    @RequestMapping(value = "/downloadtemplate.htm", method = {RequestMethod.GET})
    public HttpEntity<?> downloadTemplate(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        if (null == user) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        File targetFile = new File("static/excel/ProjectSafetyMeeting.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/ProjectSafetyMeeting.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("相关方安全会议模板", targetFile, "default.btl", map);
    }

    @ApiOperation("列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "相关方id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "meetingName", value = "会议名称" , paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页" , dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , dataType = "Integer", paramType = "query")
    })
    @RequestMapping(value = "/queryForList.htm", method = RequestMethod.GET)
    public MessageBean queryForList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String meetingName, @ApiIgnore String projectId,
                                    @ApiIgnore String enterpriseId, @ApiIgnore Integer page, @ApiIgnore Integer pageSize) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.queryForList(meetingName, projectId, enterpriseId, page, pageSize);
    }

    @ApiOperation("根据ID删除安全会议")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "String")
    })
    @RequestMapping(value = "/deleteById.htm", method = RequestMethod.POST)
    @Log(title = "根据ID删除安全会议", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean deleteById(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @ApiIgnore String id) {
        if (user == null) {
            MessageBean<?> msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return service.deleteById(id);
    }
}
